package com.zadtek.shopmall.base;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zadtek.shopmall.R;

import java.util.List;

/**
 * Created by Zackv on 2018/4/11.
 */

public abstract class AbsViewPagerActivity extends AbsActivity {
    private ImageView ivBack;
    private TextView title;
    private TabLayout tabLayout;
    protected ViewPager viewPager;
    private TabPageAdapter tabPageAdapter;

    private List<String> titles;
    private List<Fragment> fragments;
    @Override
    public int setView() {
        return R.layout.activity_viewpager;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initBaseData();
        initBaseLogic();
    }

    private void initBaseData() {
        titles = getTabTitles();
        fragments = getFragments();
        title.setText(getTopTitle());
    }


    private void initBaseLogic() {
        tabPageAdapter = new TabPageAdapter(getSupportFragmentManager(),fragments,titles);
        viewPager.setAdapter(tabPageAdapter);

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        initLogic();
    }



    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        viewPager = (ViewPager)findViewById(R.id.viewpager);
        ivBack = (ImageView) findViewById(R.id.ic_back);
        ivBack.setVisibility(View.VISIBLE);
        title = (TextView) findViewById(R.id.title);
        tabLayout.setupWithViewPager(viewPager);
    }

    private class TabPageAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragments;
        private List<String> titles;

        public TabPageAdapter(FragmentManager fm,List<Fragment> fragmentList,List<String> stringList) {
            super(fm);
            fragments = fragmentList;
            titles = stringList;
        }
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }

    public abstract String getTopTitle();

    protected abstract List<Fragment> getFragments();

    protected abstract List<String> getTabTitles();

    public  void initLogic(){

    };

}
